/* 
*** Heterogeneity of effects on clauses by subsamples of low female representation in workplace vs union
	// input: filledpanel, unionboards_SDSR_clean, estabchars_wide
	// output: TableA13.xlsx (sheet: tableA13)
*/ 

cap log close
cap log using "$logs/heterogeneity_femshare_log", replace

	
*****************
** CBA CLAUSES **
*****************

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel
	
	*** Data prep 
	* Get baseline female share in union boards and a dummy for female Pres/VP
	use "$files/unionboards_SDSR_clean.dta", clear
	keep union_id sh_female_bl female_president_bl female_vicepresident_bl union_type
	//tag unions with info on boards at baseline 
	gen has_bl = (sh_female_bl!=. & female_president_bl!=.)
	duplicates drop
	sum sh_female_bl, d 
	sum sh_female_bl if union_type=="Trabalhador", d 
	gen high_sh_female_bl = (sh_female_bl>1/3) if sh_female_bl!=.
	egen female_PorVP = rowmax( female_president_bl female_vicepresident_bl)
	tab high_sh_female_bl
	tab high_sh_female_bl if union_type=="Trabalhador"
	tab female_PorVP
	tab female_PorVP if union_type=="Trabalhador"
	tempfile baselines
	save `baselines' 

	* Use contract data
	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `no2011' {
		drop if year==2011
	}
	if `cov2014' {
		gen xxx = (total_clauses==0)&(year==2014)
		egen nocover_bl = max(xxx), by(estab_union_pair)
		unique estab_union_pair
		unique estab_union_pair if nocover_bl==0
		keep if nocover_bl==0
		drop nocover_bl xxx
	}
	
	* merge with union bl share
	cap drop _merge
	merge m:1 union_id  using `baselines' , keep(1 3)
	drop _merge
	
	* merge with estab characteristics to get share of female employment
	merge m:1 employer_id using "$files/estabchars_wide", keepusing(tot_fememployment2014 tot_employment2014)
	drop if _m == 2
	drop _merge
	replace tot_fememployment2014 = 0 if tot_fememployment2014 == . & tot_employment2014 !=.
	gen sharefemale_2014 = tot_fememployment2014/tot_employment2014
	egen unique_emp = tag(employer_id)
	sum sharefemale_2014 if unique_emp==1, d 
	local median = r(p50)
	gen high_sharefemale_2014 = (sharefemale_2014>`median') if sharefemale_2014!=.
	//for reference on baseline union characteristics restricted to the estimation sample
	egen unique_uni = tag(union_id)
	sum sh_female_bl if unique_uni==1, d 
	tab high_sh_female_bl if unique_emp==1
	tab female_PorVP if unique_emp==1

	* time varying fixed effects
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}
	
	* for clustering
	egen clustergrp = group(`clstr')
		
	* SOME COLUMNS ON TABLE WILL BE RESTRICTED TO HAVING INFO ON BOARD AT BASELINE
	tab has_bl
	unique union_id
	unique union_id if has_bl!=1 
	unique estab_union_pair
	unique estab_union_pair if has_bl!=1 
	mdesc high_sh_female_bl female_PorVP high_sharefemale_2014 
	replace female_sh = 0 if female_sh == . 
	
	* data driven method
	gen female_clauses_DD = female_clauses_DD`DDmethod'
	gen male_clauses_DD = male_clauses_DD`DDmethod'
	gen female_clauses_DD_b = female_clauses_DD`DDmethod'_b
	gen male_clauses_DD_b = male_clauses_DD`DDmethod'_b
	gen female_cl_DD_sh = female_cl_DD`DDmethod'_sh
	gen male_cl_DD_sh = male_cl_DD`DDmethod'_sh
	gen female_cl_DD_sh_b = female_cl_DD`DDmethod'_sh_b
	gen male_cl_DD_sh_b = male_cl_DD`DDmethod'_sh_b
	gen any_female_cl_DD = any_female_cl_DD`DDmethod'
	gen any_male_cl_DD = any_male_cl_DD`DDmethod'
	
	/* Interaction: % women median (establishment vs. unions)*/
	gen ishare0 = 1
	gen ishare1 = (high_sharefemale_2014==0)&(high_sh_female_bl==0)
	gen ishare2 = (high_sharefemale_2014==0)&(high_sh_female_bl==1)
	gen ishare3 = (high_sharefemale_2014==1)&(high_sh_female_bl==0)
	gen ishare4 = (high_sharefemale_2014==1)&(high_sh_female_bl==1)
	
	/*** Heterogeneity table ***/
	local depvar female_clauses_DD female_cl_DD_sh male_clauses_DD male_cl_DD_sh
	local j=1
	foreach var of local depvar {
		
		forvalues i = 0/3 {
			reghdfe `var' treatpost if ishare`i'==1, absorb(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1 & ishare`i'==1
			estadd scalar meandv=r(mean)
			estimates store p`j'`i'
		}
		local ++j
	}
	
	*** Export ***

	forvalues i = 1/4 {
		#d ;
		estout 	p`i'0  p`i'1 p`i'2 p`i'3 using "$tables/Tamenities_p`i'.txt", 
		style(tab) mlabels(none) label collabels(none)
		cells(b(star fmt(%9.3f)) se(par)) 
		stats(meandv obs, fmt(%9.2fc %9.0fc ) labels("mean depvar" "N" )) 
		drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
		#d cr
	}

	local lab1 female_clauses_DD
	local lab2 female_cl_DD_sh
	local lab3 male_clauses_DD
	local lab4 male_cl_DD_sh
	
	local r1 = 1
	local r2 = 6 
	local r3 = 11 
	local r4 = 16 
		
	import delimited "$tables/Tamenities_p1.txt", clear
	rename (v1 v2 v3 v4 v5) (`lab1' baseline both workplace union)
	export excel using "$tables/TableA13.xlsx", sheet("tableA13") cell(A`r1') firstrow(var) sheetreplace

	foreach p in 2 3 4 {
	
		import delimited "$tables/Tamenities_p`p'.txt", clear
		label var v1 `lab`p'' 
		export excel using "$tables/TableA13.xlsx", sheet("tableA13") cell(A`r`p'') firstrow(varl) sheetmodify

	}
	

cap log close
